package com.gitee.ixtf.vertx.internal

import com.gitee.ixtf.guice.Jguice
import com.gitee.ixtf.guice.kotlinx.get
import com.gitee.ixtf.vertx.ParameterConverter
import com.gitee.ixtf.vertx.internal.converter.BuildInParameterConverterProvider
import graphql.schema.DataFetchingEnvironment
import io.vertx.core.eventbus.Message
import io.vertx.ext.web.RoutingContext
import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType
import kotlin.coroutines.Continuation

interface MethodInvoker {
  suspend fun invoke(env: DataFetchingEnvironment): Any? = throw IllegalAccessError()

  suspend fun invoke(rc: RoutingContext): Any? = throw IllegalAccessError()

  suspend fun invoke(message: Message<*>): Any? = throw IllegalAccessError()
}

fun converters(method: Method): List<ParameterConverter<*>> =
    method.parameters.mapNotNull { parameter ->
      val rawType = parameter.type
      if (Continuation::class.java.isAssignableFrom(rawType)) return@mapNotNull null
      val genericType = parameter.parameterizedType as? ParameterizedType
      Jguice.get<BuildInParameterConverterProvider>()
          .getConverter(parameter, rawType, genericType, parameter.annotations)
    }
